/*
 * @(#)DataSourceFactory.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
DebugDataSource.java

This class is a wrapper class for create DebugConnection instance

  @author          Ryan Kwan
  @company         DCIVision Limited
  @creation date   02/11/2004
  @version         $Revision: 1.6 $
*/
public class DebugDataSource extends org.apache.commons.dbcp.BasicDataSource{
    
    public static final String REVISION = "$Revision: 1.6 $";

    private int mDebugLevel;
    private Map mConnections = new HashMap();
    private int mCount = 0;
    private int MAX_TRACE = 1000;

    public DebugDataSource() {
      super();	
      mDebugLevel = 2;
    }

    public void setDebugLevel(int debugLevel){
	mDebugLevel = debugLevel;
    }

    public Connection getConnection() throws SQLException {
	synchronized(mConnections){
		if(mCount == MAX_TRACE) mCount=0;
		Connection conn =  new DebugConnection(super.getConnection(),mDebugLevel);
		mConnections.put(new Integer(++mCount),conn);
		return conn;
	}
    }

    public Connection getConnection(String username, String password) throws SQLException {
	synchronized(mConnections){
		if(mCount == 1000) mCount=0;
		Connection conn =  new DebugConnection(super.getConnection(username, password),mDebugLevel);
		mConnections.put(new Integer(++mCount),conn);
		return conn;
	}
    }

    public Map getTracedConnections(){
	return mConnections;
    }

    public PrintWriter getLogWriter() throws SQLException {
        return super.getLogWriter();
    }

    public void setLogWriter(PrintWriter out) throws SQLException {
        super.setLogWriter(out);
    }

    public void setLoginTimeout(int seconds) throws SQLException {
        super.setLoginTimeout(seconds);
    }

    public int getLoginTimeout() throws SQLException {
        return super.getLoginTimeout();
    }
}